﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO ;
using System.Text.RegularExpressions;
using EnvDTE80 ;
using System.Windows.Forms;
using EnvDTE;

namespace CodeCommentAnalyser
{
    /// <summary>
    /// come
    /// </summary>
    public struct oldCommentCount
    {
        public int ThreeSlashComment
        {
            get;
            set;
        }
        public int LinesInFile
        {
            get;
            set;
        }
        public int GlobalComment
        {
            get;
            set;
        }
        public int TotalComment
        {
            get { return GlobalComment + ThreeSlashComment; }
           
        }
        public int CommentRatio
        {
            get { return (TotalComment  > 0) ? (LinesInFile / TotalComment):0 ; }
           
        }
        public string FileName
        {
            get;
            set;
        }
        public string ProjectName
        {
            get;
            set;
        }

    };

    public static class oldCsComments
    {
        readonly static string CONST_ThreeSlash = "///";
        readonly static string CONST_CsExtension = ".cs";
        static Regex regExComment = new Regex("[^/* */$]", RegexOptions.Compiled);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        internal static Tuple<bool, CommentCount> GetLineCount(string FilePath)
        {
            CommentCount commCount = new CommentCount();

            try
            {

                String[] lstLinesofCode = null;
                if (File.Exists(FilePath))
                {
                    lstLinesofCode = File.ReadAllLines(FilePath);

                    if (lstLinesofCode != null && lstLinesofCode.Length > 0)
                    {
                        commCount.LinesInFile = lstLinesofCode.Length;
                        IEnumerable<string> x = lstLinesofCode.Where(s => s.Contains(CONST_ThreeSlash));
                        if (x != null)
                        {
                           
                            commCount.ThreeSlashComment = x.Count();
                            commCount.FileName = FilePath;
                        }
                         
                        return new Tuple<bool, CommentCount>(true, commCount);
                    }
                }
            }
            catch(Exception ex)
            {
                return new Tuple<bool, CommentCount>(false, commCount);
            }
            return new Tuple<bool, CommentCount>(false, commCount);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="CurrentAppObject"></param>
        public static void GetCommentDetails(DTE2 CurrentAppObject)
        {
            MessageBox.Show("Anlayzing Project(s) !");
            MessageBox.Show(CurrentAppObject.Solution.Projects.Count.ToString());
            List<CommentCount> lstProjComments = new List<CommentCount>(); 
            foreach (EnvDTE.Project proj in CurrentAppObject.Solution.Projects)
            {
                CommentCount projComm = new CommentCount();
                projComm.ProjectName = proj.Name;
                lstProjComments.Add(projComm); 
                string strProjPath = null;
                if (!string.IsNullOrWhiteSpace(proj.FullName))
                {
                    strProjPath = System.IO.Path.GetDirectoryName(proj.FullName);
                }
                else
                {

                    strProjPath = string.Empty;
                }
                if (proj == null || proj.ProjectItems == null)
                    continue;
                int i = 0;
                    
                    foreach (EnvDTE.ProjectItem projItem in proj.ProjectItems)
                    {

                        string IndivFilePath = projItem.FileNames[0]; 
                        if (IndivFilePath.EndsWith(CONST_CsExtension, StringComparison.OrdinalIgnoreCase))
                            GetProjectComments(lstProjComments, projItem, strProjPath);
                        else
                        {
                            int itemCtr = 0;
                            EnvDTE.ProjectItem projItemInner = null;
                            do
                            {  if(projItem.SubProject != null  ) 
                                    MessageBox.Show (  projItem.SubProject.Name);
                            if ((projItem.SubProject == null) || (projItem.SubProject.ProjectItems == null) ||
                                    (projItem.SubProject.ProjectItems.Count <= ++itemCtr))
                                    break;
                                projItemInner = projItem.SubProject.ProjectItems.Item (itemCtr);
                                GetProjectComments(lstProjComments, projItemInner, strProjPath);
                            } while (projItemInner != null);
                        }
                    }
                
            }
            CommentsDetail frmCd = new CommentsDetail();
            frmCd.ListProjComments = lstProjComments;
            frmCd.ShowGrid(); 
        }

        private static void GetProjectComments(List<CommentCount> lstProjComments, EnvDTE.ProjectItem projItem, string strProjPath)
        {
                /*  CodeElements elts = projItem.FileCodeModel.CodeElements;
                  foreach (CodeElement ce in elts)
                  {
                      ce.Kind ==  vsCMElement.vsCMElementOther
                  }*/
            //MessageBox.Show(projItem.Kind  ); 
            string IndivFilePath = projItem.FileNames[0];  
           
                // MessageBox.Show(IndivFilePath);
                Tuple<bool, CommentCount> tupComm = GetLineCount(IndivFilePath);
                if (tupComm.Item1)
                {
                    lstProjComments.Add(tupComm.Item2);
                    MessageBox.Show(tupComm.Item2.ThreeSlashComment.ToString());
                }
           
        }

    }
}